function deleteCode(obj)
{
	if(obj.value == "DD.MM.YYYY" || obj.value == "teacher name" || obj.value == "class location")
		obj.value ="";
}

function clearError(idName)
{
	var elem = document.getElementById(idName);
	if (elem.className == "errorText")
		elem.innerHTML = "";
}

function checkForm()
{
	document.getElementById("courseIdText").innerHTML = "";
	document.getElementById("courseNameText").innerHTML = "";
	document.getElementById("lecturerInChargeText").innerHTML = "";
	document.getElementById("moedAText").innerHTML = "";
	document.getElementById("moedBText").innerHTML = "";
	document.getElementById("descriptionText").innerHTML = "";
	document.getElementById("capacityText").innerHTML = "";
	document.getElementById("groupNumText").innerHTML = "";
	var bool = true;
	if(document.getElementById("courseId").value == "")
		{
			document.getElementById("courseIdText").innerHTML = "insert course id";
			bool = false;
		}
	if(document.getElementById("courseName").value == "")
	{
		document.getElementById("courseNameText").innerHTML = "insert course name";
		bool = false;
	}
	if(document.getElementById("lecturerInCharge").value == "")
	{
		document.getElementById("lecturerInChargeText").innerHTML = "insert lecturer in charge";
		bool = false;
	}
	var moedA = document.getElementById("moedA").value ;
	if(moedA == "" || moedA == "DD.MM.YYYY" || dateFormat(moedA) == false)
	{
		document.getElementById("moedAText").innerHTML = "insert valid moed A date";
		bool = false;
	}	
	var moedB = document.getElementById("moedB").value ;
	if(moedB == "" || moedB == "DD.MM.YYYY" || dateFormat(moedB) == false)
	{
		document.getElementById("moedBText").innerHTML = "insert valid moed B date";
		bool = false;
	}	
	
	else if(isBefore(moedA, moedB) == false)
	{
		document.getElementById("moedBText").innerHTML = "moed B must be after moed A";
		bool = false;
	}
	if(document.getElementById("description").value == "")
	{
		document.getElementById("descriptionText").innerHTML = "insert description";
		bool = false;
	}
	if(document.getElementById("capacity").value == "")
	{
		document.getElementById("capacityText").innerHTML = "insert capacity";
		bool = false;
	}	
	if(document.getElementById("groupNum").value == "")
	{
		document.getElementById("groupNumText").innerHTML = "insert group number";
		bool = false;
	}
	
	var selectedSession = document.getElementById("numberOfSessions");
	var i=0;
	for(i=0; i<parseInt(selectedSession.value); i++)
	{
		if(document.getElementById("teacherOfSession"+i).value == "" || document.getElementById("teacherOfSession"+i).value == "teacher name")
		{
			document.getElementById("teacherOfSession"+i).value = "teacher name";
			bool = false;
		}
		if(document.getElementById("classLocationOfSession"+i).value == "" || document.getElementById("classLocationOfSession"+i).value == "class location")
		{
			document.getElementById("classLocationOfSession"+i).value = "class location";
			bool = false;
		}	
	}
	return bool;
}

function dateFormat(moed)
{
	if(moed.length < 10)
		return false;
	var i=0;
	for(i=0; i<moed.length; i++)
	{
		if(i != 2)
			if(i != 5)
					if(moed.charAt(i) == '.')
						return false;
		if(i == 2 || i == 5)
			if(moed.charAt(i) != '.')
				return false;
	}
	if(parseInt(moed.charAt(0)) > 3 || (parseInt(moed.charAt(0)) == 3 && parseInt(moed.charAt(1)) > 1))
		return false;
	if(parseInt(moed.charAt(3)) > 1 || (parseInt(moed.charAt(3)) == 1 && parseInt(moed.charAt(4)) > 2))
		return false;
	if((parseInt(moed.charAt(0)) == 0 && parseInt(moed.charAt(1)) == 0) || (parseInt(moed.charAt(3)) == 0 && parseInt(moed.charAt(4)) == 0))
		return false;
	if(parseInt(moed.charAt(3)) == 0 && parseInt(moed.charAt(4)) == 2)
		if(parseInt(moed.charAt(0)) == 3)
			return false;
		
		
	return true;
}

function isBefore(moedA, moedB)
{
	if(moedA.length != 10 || moedB.length != 10)
		return false;
	var daysA = parseInt(moedA.charAt(0))*10 + parseInt(moedA.charAt(1));
	var daysB = parseInt(moedB.charAt(0))*10 + parseInt(moedB.charAt(1));
	var monthsA = parseInt(moedA.charAt(3))*10 + parseInt(moedA.charAt(4));
	var monthsB = parseInt(moedB.charAt(3))*10 + parseInt(moedB.charAt(4));
	var yearsA = parseInt(moedA.charAt(6))*1000 + parseInt(moedA.charAt(7))*100 + parseInt(moedA.charAt(8))*10 + parseInt(moedA.charAt(9));
	var yearsB = parseInt(moedB.charAt(6))*1000 + parseInt(moedB.charAt(7))*100 + parseInt(moedB.charAt(8))*10 + parseInt(moedB.charAt(9));
	if (yearsA != yearsB)
		return (yearsB > yearsA);
	if (monthsA != monthsB)
		return (monthsB > monthsA);
	return (daysB > daysA);	
}

function changeDuration(i)
{
	var startOfSession = document.getElementById("startOfSession"+i);
	var duration2 = document.getElementById("duration2"+i);
	var duration3 = document.getElementById("duration3"+i);
	var duration4 = document.getElementById("duration4"+i);
	duration2.removeAttribute('disabled');
	duration3.removeAttribute('disabled');
	duration4.removeAttribute('disabled');
	if(startOfSession.value == "15")
	{
		duration4.setAttribute('disabled', 'disabled');
	}
	if(startOfSession.value == "16")
	{
		duration3.setAttribute('disabled', 'disabled');
		duration4.setAttribute('disabled', 'disabled');		
	}
	if(startOfSession.value == "17")
	{
		duration2.setAttribute('disabled', 'disabled');
		duration3.setAttribute('disabled', 'disabled');
		duration4.setAttribute('disabled', 'disabled');		
	}
}

function createSessions()
{
	var i=0;
	for(i=0; i<10; i++)
	{
		if(i<parseInt($("#numberOfSessions").val()))
			 $("#trOfTable"+i).fadeIn();
		else
			$("#trOfTable"+i).fadeOut();
	}
}
